package com.infosys.test.jdbc;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import javax.swing.JFrame;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.table.DefaultTableModel;

public class Gui extends skin implements ActionListener {

private final String[] titulos ={"ID","Nombre","Apellido"};
private DefaultTableModel TableM = new DefaultTableModel();
private JTable personTable = new JTable(TableM);
private BDManagement bdm = new BDManagement();

private JMenuBar menuBar;
private JMenu actionMenu;
private JMenuItem addMenuItem,removeMenuItem,readMenuItem;
private JScrollPane scroll;
private FrameRegistroPersona fap;

private List<Integer> ids = new ArrayList<Integer>();
	
public Gui(){
	super("JDBC en Java con MYSQL");
	setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
	setLayout(null);
	setSize(810,460);
	addComponents();
	setVisible(true);
	
}

public void addComponents(){ //define los componentes
	menuBar=new JMenuBar();
	this.setJMenuBar(menuBar);
	actionMenu = new JMenu("Acciones");
	menuBar.add(actionMenu);
	addMenuItem = new JMenuItem("Agregar Registro");
	removeMenuItem= new JMenuItem("Eliminar Registro");
	readMenuItem = new JMenuItem("Leer Registos");
	actionMenu.add(addMenuItem);
	actionMenu.add(removeMenuItem);
	actionMenu.add(readMenuItem);
	readMenuItem.addActionListener(this);
	addMenuItem.addActionListener(this);
	removeMenuItem.addActionListener(this);
	
	TableM.setColumnIdentifiers(titulos);
	//personTable.setEnabled(false); //es el JTable
	
	scroll = new JScrollPane(personTable);
	scroll.setBounds(0,0,800,400);
	add(scroll);
	
}
	
	
	@Override
	public void actionPerformed(ActionEvent e) {
	if(e.getSource()==addMenuItem){
		fap = new FrameRegistroPersona(this);
	
	}else if (e.getSource()==removeMenuItem){
		
	this.deleteRows(personTable.getSelectedRows());
			
	}else if (e.getSource()==readMenuItem){
		fillTable();
		
	}else if (e.getSource()==fap.btnAgregar){
		System.out.println("agregando");
		Persona p = new Persona(fap.getTxtNombre().getText(),fap.getTxtApellido().getText());
		addRow(p);
		
		fap.dispose();
	
	}
	
	}
	
public void addRow(Persona p){
	
	String query = "INSERT INTO PERSONAS (Nombre,Apellido) VALUES ('"+p.getNom()+"','"+p.getApe()+"')";
	try {
		bdm.getState().executeUpdate(query);
	} catch (SQLException e) {
		e.printStackTrace();
	}
	fillTable();
	}
	

	public void deleteRows(int[] js ){
		for (int i=0;i<js.length; i++){
			String query = "DELETE FROM personas WHERE idPersonas="+ids.get(js[i]);
			try {
				bdm.getState().executeUpdate(query);
			} catch (SQLException e) {
				System.out.println("Error al eliminar");
			}
		}
		fillTable();
	}

	
	private void fillTable() {
		TableM.setRowCount(0);
		TableM.setColumnCount(0);
		TableM.setColumnIdentifiers(titulos);
		ids.clear();
		
		try{
			ResultSet aux = bdm.getState().executeQuery("SELECT * FROM PERSONAS");
			while(aux.next()){
				Object[] fila = {aux.getObject(1),aux.getObject(2),aux.getObject(3) };
				ids.add((Integer) aux.getObject(1));
				TableM.addRow(fila);
			}
			
		}catch(Exception e){
			System.out.println("Error carga  en tabla ver: ");
			e.printStackTrace();
		}
				
	}

}
